#!/usr/bin/php
<?php
function getIPs($withV6 = true) {
    preg_match_all('/inet'.($withV6 ? '6?' : '').' addr: ?([^ ]+)/', `ifconfig`, $ips);
    return $ips[1];
}
$ips = getIPs();

$socket = stream_socket_server("udp://0.0.0.0:12345", $errno, $errstr, STREAM_SERVER_BIND);
if (!$socket) {
    die("$errstr ($errno)");
}

do {
	$pkt = stream_socket_recvfrom($socket, 512, 0, $peer);
	if ($pkt == "beerwatch/find") {
		$peer = explode(":",$peer);
		$peer = $peer[0];
		$peer = "$peer:12355";
		echo "OOOO $peer looking for me, I feel so loved!\n";
    		stream_socket_sendto($socket, $ips[2] .":80", 0, $peer);
	}

} while ($pkt !== false);

?>
